package com.cy.cy2025.activity

import android.media.session.MediaController
import android.media.session.MediaSessionManager
import android.os.Bundle
import androidx.annotation.OptIn
import androidx.appcompat.app.AppCompatActivity
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import com.cy.cy2025.databinding.ActivityMedia3TestBinding
import com.cy.cy2025.util.LogUtil
import com.cy.cy2025.util.ToastUtil


class Media3TestActivity : AppCompatActivity() {

    companion object {
        const val TAG = "Media3TestActivity"
    }

    val binding by lazy { ActivityMedia3TestBinding.inflate(layoutInflater) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)



        initPlayer1()
        initPlayer2()

    }



    private fun initPlayer1() {
        binding.player1.useController = false
        binding.player1.player = ExoPlayer.Builder(this).build()
        // 设置是否自动播放，默认为true
        binding.player1.player?.playWhenReady = true
        // 设置是否循环播放，默认为false
        binding.player1.player?.repeatMode = Player.REPEAT_MODE_ONE
        // 设置播放音量(不跟随设备音量)
        binding.player1.player?.volume = 0f
        binding.player1.player?.addListener(object : Player.Listener {

            override fun onIsPlayingChanged(isPlaying: Boolean) {
                super.onIsPlayingChanged(isPlaying)
                // 播放状态变化回调
            }

            override fun onPlaybackStateChanged(playbackState: Int) {
                super.onPlaybackStateChanged(playbackState)
                when (playbackState) {
                    Player.STATE_IDLE -> {
                        //播放器停止时的状态
                        LogUtil.d(TAG,"播放器停止时的状态")
                    }

                    Player.STATE_BUFFERING -> {
                        // 正在缓冲数据
                        LogUtil.d(TAG,"正在缓冲数据")
                    }

                    Player.STATE_READY -> {
                        // 可以开始播放
                        LogUtil.d(TAG,"可以开始播放")
                        //binding.player1.player?.play()
                    }

                    Player.STATE_ENDED -> {
                        // 播放结束
                        LogUtil.d(TAG,"播放结束")
                    }
                }
            }

            override fun onPlayerError(error: PlaybackException) {
                super.onPlayerError(error)
                LogUtil.d(TAG,"播放出错")

            }

        })

        binding.player1.player?.run {
            setMediaItem(
                MediaItem.Builder()
                    .setUri("https://www.runoob.com/try/demo_source/movie.mp4")
                    .build()
            )
            prepare()
        }

        binding.btnPlay1.setOnClickListener {
            binding.player1.player?.play()
        }

        binding.btnPause1.setOnClickListener {
            binding.player1.player?.pause()
        }

    }

    @OptIn(UnstableApi::class)
    private fun initPlayer2() {
        binding.player2.player = ExoPlayer.Builder(this).build()
        //显示进度条
        binding.player2.apply {
            useController = true // 启用控制器（默认就是true，可省略）

            // 2. 控制器行为配置
            controllerAutoShow = true // 播放时自动显示控制器，闲置后隐藏
            controllerShowTimeoutMs = 3000 // 控制器自动隐藏的延迟（毫秒）
            setControllerVisibilityListener(object : PlayerView.ControllerVisibilityListener {
                override fun onVisibilityChanged(visibility: Int) {
                    LogUtil.d(TAG, "控制器可见性变化：$visibility")
                }
            })
            setControllerHideDuringAds(true) // 广告期间隐藏控制器
            // 增加触摸监听器，手动显示控制器
            setOnClickListener {
                ToastUtil.showShort("控制器显示")
                showController()
            }

            binding.player2.player?.run {
                setMediaItem(
                    MediaItem.Builder()
                        .setUri("https://www.runoob.com/try/demo_source/movie.mp4")
                        .build()
                )
                prepare()
            }

            binding.btnPlay2.setOnClickListener {
                binding.player2.player?.play()
            }

            binding.btnPause2.setOnClickListener {
                binding.player2.player?.pause()
            }

        }
    }


    override fun onResume() {
        super.onResume()
        // 恢复播放
        binding.player1.onResume()
        binding.player2.onResume()
    }

    override fun onPause() {
        super.onPause()
        LogUtil.d(TAG,"onPause")
        // 暂停播放
        binding.player1.onPause()
        binding.player2.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        // 释放播放器资源
        binding.player1.player?.release()
        binding.player1.player = null

        // 释放播放器资源
        binding.player2.player?.release()
        binding.player2.player = null
    }
}